#!/usr/bin/env python
# -*- coding:utf-8 -*-

# python3.5
# https://zh.osdn.net/projects/sfnet_pyqt/releases/  下载  PyQt5-5.6-gpl-Py3.5-Qt5.6.0-x32-2.exe


# http://blog.csdn.net/pdcxs007/article/details/45095293/
# PyQt5初次体验

from PyQt5 import QtWidgets
import sys


class LoginDlg(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(LoginDlg, self).__init__(parent)
        usr = QtWidgets.QLabel("用户：")
        pwd = QtWidgets.QLabel("密码：")
        self.usrLineEdit = QtWidgets.QLineEdit()
        self.pwdLineEdit = QtWidgets.QLineEdit()
        self.pwdLineEdit.setEchoMode(QtWidgets.QLineEdit.Password)

        gridLayout = QtWidgets.QGridLayout()
        gridLayout.addWidget(usr, 0, 0, 1, 1)
        gridLayout.addWidget(pwd, 1, 0, 1, 1)
        gridLayout.addWidget(self.usrLineEdit, 0, 1, 1, 3)
        gridLayout.addWidget(self.pwdLineEdit, 1, 1, 1, 3)

        okBtn = QtWidgets.QPushButton("确定")
        cancelBtn = QtWidgets.QPushButton("取消")
        btnLayout = QtWidgets.QHBoxLayout()

        btnLayout.setSpacing(60)
        btnLayout.addWidget(okBtn)
        btnLayout.addWidget(cancelBtn)

        dlgLayout = QtWidgets.QVBoxLayout()
        dlgLayout.setContentsMargins(40, 40, 40, 40)
        dlgLayout.addLayout(gridLayout)
        dlgLayout.addStretch(40)
        dlgLayout.addLayout(btnLayout)

        self.setLayout(dlgLayout)
        okBtn.clicked.connect(self.accept)
        cancelBtn.clicked.connect(self.reject)
        self.setWindowTitle("登录")
        self.resize(300, 200)

    def accept(self):
        if self.usrLineEdit.text().strip() == "eric" and self.pwdLineEdit.text() == "eric":
            super(LoginDlg, self).accept()
        else:
            QtWidgets.QMessageBox.warning(self, "警告", "用户名或密码错误！", QMessageBox.Yes)
            self.usrLineEdit.setFocus()


def main():
    app = QtWidgets.QApplication(sys.argv)
    dlg = LoginDlg()
    dlg.show()
    dlg.exec_()
    app.exit()


if __name__ == "__main__":
    main()
